<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>处方流转订单管理 API 测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
        }
        .test-section h3 {
            margin-top: 0;
            color: #1890ff;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #666;
        }
        input, select {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
        button {
            background-color: #1890ff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            margin-right: 10px;
            margin-bottom: 10px;
        }
        button:hover {
            background-color: #40a9ff;
        }
        .result {
            margin-top: 15px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 4px;
            border-left: 4px solid #1890ff;
            white-space: pre-wrap;
            font-family: monospace;
            font-size: 12px;
            max-height: 400px;
            overflow-y: auto;
        }
        .error {
            border-left-color: #f5222d;
            background-color: #fff1f0;
        }
        .success {
            border-left-color: #52c41a;
            background-color: #f6ffed;
        }
        .token-section {
            background-color: #fff7e6;
            border: 1px solid #ffd591;
        }
        .api-url {
            font-family: monospace;
            background-color: #f0f0f0;
            padding: 2px 6px;
            border-radius: 3px;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>处方流转订单管理 API 测试</h1>
        
        <!-- Token 管理 -->
        <div class="test-section token-section">
            <h3>🔑 Token 管理</h3>
            <div class="form-group">
                <label>JWT Token:</label>
                <input type="text" id="tokenInput" placeholder="输入JWT Token" style="font-family: monospace; font-size: 12px;">
            </div>
            <button onclick="setToken()">设置 Token</button>
            <button onclick="clearToken()">清除 Token</button>
            <div class="result" id="tokenResult"></div>
        </div>

        <!-- 获取处方流转订单列表 -->
        <div class="test-section">
            <h3>📋 获取处方流转订单列表</h3>
            <div class="form-group">
                <label>患者姓名或手机号:</label>
                <input type="text" id="patientNameOrPhone" placeholder="输入患者姓名或手机号">
            </div>
            <div class="form-group">
                <label>开始时间:</label>
                <input type="datetime-local" id="startTime">
            </div>
            <div class="form-group">
                <label>结束时间:</label>
                <input type="datetime-local" id="endTime">
            </div>
            <div class="form-group">
                <label>订单状态:</label>
                <select id="status">
                    <option value="">全部</option>
                    <option value="0">待支付</option>
                    <option value="1">已完成</option>
                    <option value="2">已失效</option>
                </select>
            </div>
            <div class="form-group">
                <label>页码:</label>
                <input type="number" id="pageIndex" value="1" min="1">
            </div>
            <div class="form-group">
                <label>每页大小:</label>
                <input type="number" id="pageSize" value="10" min="1" max="100">
            </div>
            <button onclick="getPrescriptionOrderList()">获取列表</button>
            <div class="result" id="listResult"></div>
        </div>

        <!-- 获取处方流转订单详情 -->
        <div class="test-section">
            <h3>📄 获取处方流转订单详情</h3>
            <div class="form-group">
                <label>订单ID:</label>
                <input type="number" id="orderId" placeholder="输入订单ID">
            </div>
            <button onclick="getPrescriptionOrderDetail()">获取详情</button>
            <div class="result" id="detailResult"></div>
        </div>

        <!-- 获取统计信息 -->
        <div class="test-section">
            <h3>📊 获取统计信息</h3>
            <button onclick="getPrescriptionOrderStatistics()">获取统计</button>
            <div class="result" id="statisticsResult"></div>
        </div>

        <!-- 删除订单 -->
        <div class="test-section">
            <h3>🗑️ 删除订单</h3>
            <div class="form-group">
                <label>订单ID:</label>
                <input type="number" id="deleteOrderId" placeholder="输入要删除的订单ID">
            </div>
            <button onclick="deletePrescriptionOrder()">删除订单</button>
            <div class="result" id="deleteResult"></div>
        </div>

        <!-- 批量删除订单 -->
        <div class="test-section">
            <h3>🗑️ 批量删除订单</h3>
            <div class="form-group">
                <label>订单ID列表 (用逗号分隔):</label>
                <input type="text" id="batchDeleteIds" placeholder="例如: 1,2,3">
            </div>
            <button onclick="batchDeletePrescriptionOrders()">批量删除</button>
            <div class="result" id="batchDeleteResult"></div>
        </div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:5242/api';
        let currentToken = '';

        // Token 管理
        function setToken() {
            const token = document.getElementById('tokenInput').value.trim();
            if (token) {
                currentToken = token;
                localStorage.setItem('jwt_token', token);
                showResult('tokenResult', 'Token 设置成功！', 'success');
            } else {
                showResult('tokenResult', '请输入有效的 Token', 'error');
            }
        }

        function clearToken() {
            currentToken = '';
            localStorage.removeItem('jwt_token');
            document.getElementById('tokenInput').value = '';
            showResult('tokenResult', 'Token 已清除', 'success');
        }

        // 页面加载时恢复 Token
        window.onload = function() {
            const savedToken = localStorage.getItem('jwt_token');
            if (savedToken) {
                currentToken = savedToken;
                document.getElementById('tokenInput').value = savedToken;
            }
        };

        // 通用请求函数
        async function makeRequest(url, options = {}) {
            try {
                const headers = {
                    'Content-Type': 'application/json',
                    ...options.headers
                };

                if (currentToken) {
                    headers['Authorization'] = `Bearer ${currentToken}`;
                }

                const response = await fetch(url, {
                    ...options,
                    headers
                });

                const data = await response.json();
                return { success: response.ok, data, status: response.status };
            } catch (error) {
                return { success: false, error: error.message };
            }
        }

        // 显示结果
        function showResult(elementId, message, type = '') {
            const element = document.getElementById(elementId);
            element.textContent = message;
            element.className = `result ${type}`;
        }

        // 获取处方流转订单列表
        async function getPrescriptionOrderList() {
            const params = new URLSearchParams();
            
            const patientNameOrPhone = document.getElementById('patientNameOrPhone').value;
            const startTime = document.getElementById('startTime').value;
            const endTime = document.getElementById('endTime').value;
            const status = document.getElementById('status').value;
            const pageIndex = document.getElementById('pageIndex').value;
            const pageSize = document.getElementById('pageSize').value;

            if (patientNameOrPhone) params.append('patientNameOrPhone', patientNameOrPhone);
            if (startTime) params.append('startTime', new Date(startTime).toISOString());
            if (endTime) params.append('endTime', new Date(endTime).toISOString());
            if (status) params.append('status', status);
            params.append('pageIndex', pageIndex);
            params.append('pageSize', pageSize);

            const url = `${API_BASE_URL}/PrescriptionOrder/list?${params.toString()}`;
            
            showResult('listResult', '请求中...', '');
            
            const result = await makeRequest(url);
            
            if (result.success) {
                showResult('listResult', 
                    `✅ 请求成功\n\nURL: ${url}\n\n响应数据:\n${JSON.stringify(result.data, null, 2)}`, 
                    'success'
                );
            } else {
                showResult('listResult', 
                    `❌ 请求失败\n\nURL: ${url}\n\n错误信息:\n${JSON.stringify(result, null, 2)}`, 
                    'error'
                );
            }
        }

        // 获取处方流转订单详情
        async function getPrescriptionOrderDetail() {
            const orderId = document.getElementById('orderId').value;
            if (!orderId) {
                showResult('detailResult', '请输入订单ID', 'error');
                return;
            }

            const url = `${API_BASE_URL}/PrescriptionOrder/detail/${orderId}`;
            
            showResult('detailResult', '请求中...', '');
            
            const result = await makeRequest(url);
            
            if (result.success) {
                showResult('detailResult', 
                    `✅ 请求成功\n\nURL: ${url}\n\n响应数据:\n${JSON.stringify(result.data, null, 2)}`, 
                    'success'
                );
            } else {
                showResult('detailResult', 
                    `❌ 请求失败\n\nURL: ${url}\n\n错误信息:\n${JSON.stringify(result, null, 2)}`, 
                    'error'
                );
            }
        }

        // 获取统计信息
        async function getPrescriptionOrderStatistics() {
            const url = `${API_BASE_URL}/PrescriptionOrder/statistics`;
            
            showResult('statisticsResult', '请求中...', '');
            
            const result = await makeRequest(url);
            
            if (result.success) {
                showResult('statisticsResult', 
                    `✅ 请求成功\n\nURL: ${url}\n\n响应数据:\n${JSON.stringify(result.data, null, 2)}`, 
                    'success'
                );
            } else {
                showResult('statisticsResult', 
                    `❌ 请求失败\n\nURL: ${url}\n\n错误信息:\n${JSON.stringify(result, null, 2)}`, 
                    'error'
                );
            }
        }

        // 删除订单
        async function deletePrescriptionOrder() {
            const orderId = document.getElementById('deleteOrderId').value;
            if (!orderId) {
                showResult('deleteResult', '请输入订单ID', 'error');
                return;
            }

            const url = `${API_BASE_URL}/PrescriptionOrderWrite/delete`;
            const data = { id: parseInt(orderId) };
            
            showResult('deleteResult', '请求中...', '');
            
            const result = await makeRequest(url, {
                method: 'POST',
                body: JSON.stringify(data)
            });
            
            if (result.success) {
                showResult('deleteResult', 
                    `✅ 删除成功\n\nURL: ${url}\n\n请求数据: ${JSON.stringify(data)}\n\n响应数据:\n${JSON.stringify(result.data, null, 2)}`, 
                    'success'
                );
            } else {
                showResult('deleteResult', 
                    `❌ 删除失败\n\nURL: ${url}\n\n请求数据: ${JSON.stringify(data)}\n\n错误信息:\n${JSON.stringify(result, null, 2)}`, 
                    'error'
                );
            }
        }

        // 批量删除订单
        async function batchDeletePrescriptionOrders() {
            const idsInput = document.getElementById('batchDeleteIds').value;
            if (!idsInput) {
                showResult('batchDeleteResult', '请输入订单ID列表', 'error');
                return;
            }

            const ids = idsInput.split(',').map(id => parseInt(id.trim())).filter(id => !isNaN(id));
            if (ids.length === 0) {
                showResult('batchDeleteResult', '请输入有效的订单ID列表', 'error');
                return;
            }

            const url = `${API_BASE_URL}/PrescriptionOrderWrite/batch-delete`;
            const data = { ids };
            
            showResult('batchDeleteResult', '请求中...', '');
            
            const result = await makeRequest(url, {
                method: 'POST',
                body: JSON.stringify(data)
            });
            
            if (result.success) {
                showResult('batchDeleteResult', 
                    `✅ 批量删除成功\n\nURL: ${url}\n\n请求数据: ${JSON.stringify(data)}\n\n响应数据:\n${JSON.stringify(result.data, null, 2)}`, 
                    'success'
                );
            } else {
                showResult('batchDeleteResult', 
                    `❌ 批量删除失败\n\nURL: ${url}\n\n请求数据: ${JSON.stringify(data)}\n\n错误信息:\n${JSON.stringify(result, null, 2)}`, 
                    'error'
                );
            }
        }
    </script>
</body>
</html> 